<?php
require_once('functions/pageLoad.php');
loginRequired();
$user_session = explode("_", $_SESSION['user']);
$user_id = $user_session[0];
$page_title = 'Notification settings';

if($_SERVER['REQUEST_METHOD'] == 'POST'){


	$sql = "UPDATE users SET newsletter = '".mysql_real_escape_string($_POST['newsletter'])."', approvals = '".mysql_real_escape_string($_POST['approved'])."', quotations = '".mysql_real_escape_string($_POST['quotes'])."', questions = '".mysql_real_escape_string($_POST['questions'])."', new_jobs = '".mysql_real_escape_string($_POST['jobs'])."', custom_quotes = '".mysql_real_escape_string($_POST['custom'])."', accepted_quotes = '".mysql_real_escape_string($_POST['accepted'])."', new_quotation = '".mysql_real_escape_string($_POST['new_quotations'])."', new_question = '".mysql_real_escape_string($_POST['new_questions'])."' WHERE id = '".$user_id."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

	$results = '<p style="color: #009900;"><strong>Your notification settings have been updated successfully!</strong></p>';

}

	$sql = "SELECT newsletter, approvals, quotations, questions, mobile, new_jobs, custom_quotes, accepted_quotes, mobile_verified, new_quotation, new_question FROM users WHERE id = '".$user_id."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$rs=mysql_fetch_assoc($query);

if(strlen($rs['mobile']) == 0){

$mobile_content = '<div class="yellow_box" style="margin-top: 10px; margin-bottom: 5px;"><p><strong>You have not specified a mobile phone number.</strong></p></div>';

}

elseif($rs['mobile_verified'] == '0'){

$mobile_content = '<div class="yellow_box" style="margin-top: 10px; margin-bottom: 5px;"><p>Your current mobile number is: <strong>'.$rs['mobile'].'</strong> and is <span style="color: #cc0000;"><strong>not verified</strong></span>. <a onclick="return verifyMobile();" style="cursor: pointer; text-decoration: underline;">Click here to verify your mobile</a></p></div>';

}

else{

$mobile_content = '<div class="yellow_box" style="margin-top: 10px; margin-bottom: 5px;"><p>Your current mobile number is: <strong>'.$rs['mobile'].'</strong> and is <span style="color: #009900;"><strong>verified</strong></span>.</p></div>';

}

if($_GET['mobile'] == 'verified'){

$results = '<p style="color: #009900;"><strong>Your mobile number was successfully verified!</strong></p>';

}

include('includes/meta.php');
include('includes/header.php');
include('includes/navigation.php');

?>

<h1><?php echo $page_title; ?></h1>

<?php echo $results; ?>

<p class="noTop">Please make the relevant changes below to update the way we contact you.</p>

<form method="post" action="/notification-settings" id="notification-settings">

<div id="registration_form">

<input type="hidden" id="form" name="form" value="notification-settings" style="display: none;" />

<h2>Email notifications</h2>

<div class="rowWrapper" id="newsletter_wrap" style="margin-top: 15px;">
<div class="rowFull">
<input type="checkbox" name="newsletter" id="newsletter" value="1"<?php if($rs['newsletter'] == '1'){ echo ' checked="checked"'; } ?> />
<label for="newsletter">Please send me regular emails containing site updates and news.</label>
</div>
</div>

<div class="rowWrapper" id="approved_wrap">
<div class="rowFull">
<input type="checkbox" name="approved" id="approved" value="1"<?php if($rs['approvals'] == '1'){ echo ' checked="checked"'; } ?> />
<label for="approved">Please notify me by email whenever a job I have posted has been approved and gone live on the web site.</label>
</div>
</div>

<div class="rowWrapper" id="quotes_wrap">
<div class="rowFull">
<input type="checkbox" name="quotes" id="quotes" value="1"<?php if($rs['quotations'] == '1'){ echo ' checked="checked"'; } ?> />
<label for="quotes">Please notify me by email whenever I receive a quotation for a job I have posted.</label>
</div>
</div>

<div class="rowWrapper" id="questions_wrap">
<div class="rowFull">
<input type="checkbox" name="questions" id="questions" value="1"<?php if($rs['questions'] == '1'){ echo ' checked="checked"'; } ?> />
<label for="questions">Please notify me by email whenever I receive a question about a job I have posted.</label>
</div>
</div>


<div style="float: left; width: 600px">
<h2>Mobile notifications</h2>

<?php echo $mobile_content; ?>
<p>To change your mobile number, go to the <?php echo ($_SESSION['tradesman'] == '1') ? '<a href="/edit-company-profile">edit company profile</a>' : '<a href="/personal-details">personal details</a>' ; ?> page.</p>

</div>

<?php if($_SESSION['tradesman'] == '1'){ ?>

<div class="rowWrapper" id="jobs_wrap">
<div class="rowFull">
<input type="checkbox" name="jobs" id="jobs" value="1"<?php if($rs['new_jobs'] == '1'){ echo ' checked="checked"'; } ?> />
<label for="jobs">Please notify me by SMS text message whenever a new job I can quote for is posted.</label>
</div>
</div>

<div class="rowWrapper" id="custom_wrap">
<div class="rowFull">
<input type="checkbox" name="custom" id="custom" value="1"<?php if($rs['custom_quotes'] == '1'){ echo ' checked="checked"'; } ?> />
<label for="custom">Please notify me by SMS text message whenever I have been specifically requested to quote for a job by a user.</label>
</div>
</div>

<div class="rowWrapper" id="accepted_wrap">
<div class="rowFull">
<input type="checkbox" name="accepted" id="accepted" value="1"<?php if($rs['accepted_quotes'] == '1'){ echo ' checked="checked"'; } ?> />
<label for="accepted">Please notify me by SMS text message whenever a quote I have posted has been accepted.</label>
</div>
</div>

<?php } ?>

<div class="rowWrapper" id="new_quotations_wrap">
<div class="rowFull">
<input type="checkbox" name="new_quotations" id="new_quotations" value="1"<?php if($rs['new_quotation'] == '1'){ echo ' checked="checked"'; } ?> />
<label for="new_quotations">Please notify me by SMS text message whenever I receive a quotation for a job I have posted.</label>
</div>
</div>

<div class="rowWrapper" id="new_questions_wrap">
<div class="rowFull">
<input type="checkbox" name="new_questions" id="new_questions" value="1"<?php if($rs['new_question'] == '1'){ echo ' checked="checked"'; } ?> />
<label for="new_questions">Please notify me by SMS text message whenever I receive a question for a job I have posted.</label>
</div>
</div>

<input type="image" src="/images/update_button.gif" alt="Update" />

</div>

</form>

<?php 

include('includes/rightColumn.php');
include('includes/footer.php');

?>